<template>
    <h2>用户添加借款信息</h2>
    <table class="table table-bordered">
        <tr>
            <td>借款名称</td>
            <td>{{ info.Bname }}</td>
        </tr>
        <tr>
            <td>借款最高金额</td>
            <td>{{ info.BmaxPrice }}</td>
        </tr>
        <tr>
            <td>借款年限</td>
            <td>{{ info.Byear }}</td>
        </tr>
        <tr>
            <td>利息</td>
            <td>{{ info.Ill }}</td>
        </tr>
        <tr>
            <td>借款简介</td>
            <td>{{ info.Binfo }}</td>
        </tr>
        <tr>
            <td>用户借款金额</td>
            <td>
                <input type="text"  placeholder="请输入借款金额" v-model="BU_Money">
            </td>
        </tr>
        <tr>
            <td colspan="2">
             <button @click="Add">提交</button>
            </td>
           
        </tr>
    
    </table>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
import { useStore } from '../../store/index.ts';


const store = useStore();
const router = useRouter();
const route = useRoute();


const BU_Money=ref(0);
const info = ref({
    Bid:0,
    Bname:'',
    BmaxPrice:0,
    Byear:0,
    Iid:0,
    Binfo:'',
    Btime:'',
    Ill:'',
});

onMounted(() => {
    Show();
})





const Show = () => {
    axios({
        method: 'get',
        url: 'http://localhost:61498/api/Borrow/SelBorrowInfo',
        params: {
            Bid: route.query.Bid,
        }
    }).then(res => {
        info.value = res.data[0];
     
    })
       
        .catch(err => {
            console.log(err);
        })
}

const Add = () => {
    if(Number(BU_Money.value)<0 )
    {
        alert("请输入正确的金额");
        return;
    }
    if(Number(BU_Money.value)>info.value.BmaxPrice)
    {
        alert("输入的金额不能大于最高金额");
        return;
    }
    axios({
        method: 'post',
        url: 'http://localhost:61498/api/Borrow/AddBorrowInfo',
        data: {
            Bid: route.query.Bid,
            Uid:store.Uid,
            BU_Money:BU_Money.value,
            
        }
    }).then(res => {
        if(res.data>0)
        {
            alert("借款成功");
            router.push("/UserShowBorrow");
        }
        else
        {
            alert("借款失败");
        }
    })
        .catch(err => {
            console.log(err);
        })
}
</script>